package com.java110.mall.cart.storeOrderEngine;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.java110.core.client.FtpUploadTemplate;
import com.java110.core.client.OssUploadTemplate;
import com.java110.core.client.RestTemplate;
import com.java110.core.log.LoggerFactory;
import com.java110.dto.file.FileDto;
import com.java110.dto.integral.IntegralDto;
import com.java110.dto.integral.PreStoreUserIntegralDto;
import com.java110.dto.smallWeChat.SmallWeChatDto;
import com.java110.dto.storeOrder.StoreOrderDto;
import com.java110.dto.storeOrder.StoreOrderIntegralDto;
import com.java110.dto.storeOrder.StoreOrderLedgerAcctDto;
import com.java110.dto.user.UserDto;
import com.java110.intf.acct.*;
import com.java110.intf.cart.IStoreOrderInnerServiceSMO;
import com.java110.intf.cart.IStoreOrderIntegralV1InnerServiceSMO;
import com.java110.intf.cart.IStoreOrderLedgerAcctV1InnerServiceSMO;
import com.java110.intf.system.ISmallWechatV1InnerServiceSMO;
import com.java110.intf.user.IUserV1InnerServiceSMO;
import com.java110.po.storeOrder.StoreOrderPo;
import com.java110.utils.cache.MappingCache;
import com.java110.utils.constant.MappingConstant;
import com.java110.utils.constant.WechatConstant;
import com.java110.utils.lock.DistributedLock;
import com.java110.utils.util.*;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.ssl.SSLContexts;
import org.apache.http.util.EntityUtils;
import org.slf4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;

import javax.net.ssl.SSLContext;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.security.KeyStore;
import java.util.*;

//分账微信 文档 https://pay.weixin.qq.com/wiki/doc/api/allocation_sl.php?chapter=25_1&index=1
//v2 每次都找不到

@Component
public class StoreOrderShareAcct {
    private static final Logger logger = LoggerFactory.getLogger(StoreOrderShareAcct.class);

    //微信支付
    public static final String DOMAIN_WECHAT_PAY = "WECHAT_PAY";
    // 微信服务商支付开关
    public static final String WECHAT_SERVICE_PAY_SWITCH = "WECHAT_SERVICE_PAY_SWITCH";

    private static final String WECHAT_SERVICE_APP_ID = "SERVICE_APP_ID";

    private static final String WECHAT_SERVICE_MCH_ID = "SERVICE_MCH_ID";

    //开关ON打开
    public static final String WECHAT_SERVICE_PAY_SWITCH_ON = "ON";

    // 添加分账接收方API
    private static final String ADD_RECEIVER_URL = "https://api.mch.weixin.qq.com/pay/profitsharingaddreceiver";
    private static final String FINISH_RECEIVER_URL = "https://api.mch.weixin.qq.com/secapi/pay/profitsharingfinish";

    // 删除分账接收方api
    private static final String DELETE_RECEIVER_URL = "https://api.mch.weixin.qq.com/pay/profitsharingremovereceiver";

    // 请求分账API
    private static final String PROFIT_SHARING_URL = "https://api.mch.weixin.qq.com/secapi/pay/profitsharing";

    @Autowired
    private IStoreOrderLedgerAcctV1InnerServiceSMO storeOrderLedgerAcctV1InnerServiceSMOImpl;


    @Autowired
    private IAccountInnerServiceSMO accountInnerServiceSMOImpl;
    @Autowired
    private ICouponPoolV1InnerServiceSMO couponPoolV1InnerServiceSMOImpl;

    @Autowired
    private ICouponUserV1InnerServiceSMO couponUserV1InnerServiceSMOImpl;
    @Autowired
    private IUserV1InnerServiceSMO userV1InnerServiceSMOImpl;

    @Autowired
    private IStoreOrderInnerServiceSMO storeOrderInnerServiceSMOImpl;

    @Autowired
    private RestTemplate outRestTemplate;

    @Autowired
    private ISmallWechatV1InnerServiceSMO smallWechatV1InnerServiceSMOImpl;

    @Autowired
    private FtpUploadTemplate ftpUploadTemplate;

    @Autowired
    private OssUploadTemplate ossUploadTemplate;

    @Autowired
    private IGoldV1InnerServiceSMO goldV1InnerServiceSMOImpl;

    @Autowired
    private IIntegralV1InnerServiceSMO integralV1InnerServiceSMOImpl;

    @Autowired
    private IStoreOrderIntegralV1InnerServiceSMO storeOrderIntegralV1InnerServiceSMOImpl;

    /**
     * 分账处理接口
     *
     * @param storeOrderDto
     */

    public void shareAcct(StoreOrderDto storeOrderDto) {

        //查询分账表
        StoreOrderLedgerAcctDto storeOrderLedgerAcctDto = new StoreOrderLedgerAcctDto();
        storeOrderLedgerAcctDto.setOrderId(storeOrderDto.getOrderId());
        List<StoreOrderLedgerAcctDto> storeOrderLedgerAcctDtos = storeOrderLedgerAcctV1InnerServiceSMOImpl.queryStoreOrderLedgerAccts(storeOrderLedgerAcctDto);
        if (ListUtil.isNull(storeOrderLedgerAcctDtos)) {
            return;
        }

        //说明已经分账过
        String requestId = DistributedLock.getLockUUID();
        String key = this.getClass().getSimpleName() + storeOrderDto.getOrderId();
        try {
            DistributedLock.waitGetDistributedLock(key, requestId);
            if (!StoreOrderDto.PROFIT_SHARING_WAIT.equals(storeOrderDto.getProfitSharing())) {
                return;
            }

            //查询分账表
            storeOrderLedgerAcctDto = new StoreOrderLedgerAcctDto();
            storeOrderLedgerAcctDto.setOrderId(storeOrderDto.getOrderId());
            storeOrderLedgerAcctDtos = storeOrderLedgerAcctV1InnerServiceSMOImpl.queryStoreOrderLedgerAccts(storeOrderLedgerAcctDto);
            if (ListUtil.isNull(storeOrderLedgerAcctDtos)) {
                return;
            }

            //todo 抵扣金充到小区的 金币账户，并且调用 物业系统将金额充到业主账户里
            preStoreCommunityGold(storeOrderLedgerAcctDtos);

            //todo 赠送积分，存到用户的积分账户里
            prePersonIntegral(storeOrderLedgerAcctDtos);

            String payOrderId = storeOrderDto.getPayOrderId();

            if (StringUtil.isEmpty(payOrderId)) { //分账失败，没有支付ID
                return;
            }
            String payPlatform = MappingCache.getValue(MappingConstant.WECHAT_STORE_DOMAIN, "PAY_PLATFORM");

            //todo 支付到平台 直接返回
            if ("ON".equals(payPlatform)) {
                return;
            }

            SmallWeChatDto smallWeChatDto = new SmallWeChatDto();
            smallWeChatDto.setObjId(storeOrderLedgerAcctDtos.get(0).getShopId());
            List<SmallWeChatDto> smallWeChatDtos = smallWechatV1InnerServiceSMOImpl.querySmallWechats(smallWeChatDto);

            Assert.listOnlyOne(smallWeChatDtos, "商家未配置商户信息");

            smallWeChatDto = smallWeChatDtos.get(0);

            //调用微信分账
            String appIdCache = MappingCache.getValue(WechatConstant.WECHAT_DOMAIN, "appId");
            String appSecretCache = MappingCache.getValue(WechatConstant.WECHAT_DOMAIN, "appSecret");
            smallWeChatDto.setAppId(appIdCache);
            smallWeChatDto.setAppSecret(appSecretCache);
            // 添加接收方
            JSONArray params = AddReceivers(smallWeChatDto, storeOrderLedgerAcctDtos);

            //请求分账
            JSONObject applyResult = applyProfileShare(storeOrderDto, smallWeChatDto, params);

            //分账成功
            if (applyResult.containsKey("result_code") && "SUCCESS".equals(applyResult.getString("result_code"))) {
                JSONObject finishParam = new JSONObject();
                finishParam.put("out_order_no", applyResult.getString("order_id"));
                //完结分账 单次分账
                //finishProfileShare(storeOrderDto, smallWeChatDto, finishParam);
            } else {
                throw new IllegalArgumentException("分账失败:" + applyResult.toJSONString());
            }

            // 删除接收方
            DeleteReceivers(smallWeChatDto, storeOrderLedgerAcctDtos);
            //修改为分账完成
            StoreOrderPo storeOrderPo = new StoreOrderPo();
            storeOrderPo.setOrderId(storeOrderDto.getOrderId());
            storeOrderPo.setProfitSharing(StoreOrderDto.PROFIT_SHARING_FINISH);
            storeOrderInnerServiceSMOImpl.updateStoreOrder(storeOrderPo);
        } catch (Exception e) {
            logger.error("分账异常", e);
            throw new IllegalArgumentException(e.getMessage());
        } finally {
            DistributedLock.releaseDistributedLock(key, requestId);
        }
    }


    private JSONObject applyProfileShare(StoreOrderDto storeOrderDto, SmallWeChatDto smallWeChatDto, JSONArray params) throws Exception {
        SortedMap<String, String> paramMap = new TreeMap<String, String>();
        paramMap.put("appid", smallWeChatDto.getAppId());
        paramMap.put("mch_id", smallWeChatDto.getMchId());
        paramMap.put("nonce_str", PayUtil.makeUUID(32));
        paramMap.put("transaction_id", storeOrderDto.getPayOrderId());
        paramMap.put("out_order_no", "FZ" + storeOrderDto.getOrderId());

        String paySwitch = MappingCache.getValue(DOMAIN_WECHAT_PAY, WECHAT_SERVICE_PAY_SWITCH);
        if (WECHAT_SERVICE_PAY_SWITCH_ON.equals(paySwitch)) {
            paramMap.put("appid", MappingCache.getValue(DOMAIN_WECHAT_PAY, WECHAT_SERVICE_APP_ID));  //服务商appid，是服务商注册时公众号的id
            paramMap.put("mch_id", MappingCache.getValue(DOMAIN_WECHAT_PAY, WECHAT_SERVICE_MCH_ID));  //服务商商户号
            //paramMap.put("sub_appid", smallWeChatDto.getAppId());//起调小程序appid
            paramMap.put("sub_mch_id", smallWeChatDto.getMchId());//起调小程序的商户号
        }
        paramMap.put("receivers", params.toJSONString());
        paramMap.put("sign", PayUtil.createSignSha256(paramMap, smallWeChatDto.getPayPassword()));
        //转换为xml
        String xmlData = PayUtil.mapToXml(paramMap);

        JSONObject paramOut = request(PROFIT_SHARING_URL, xmlData, smallWeChatDto, paramMap.get("mch_id"));
        logger.debug("请求分账 信息{}，返回分账信息{}", xmlData, paramOut.toJSONString());
        return paramOut;

    }

    private void finishProfileShare(StoreOrderDto storeOrderDto, SmallWeChatDto smallWeChatDto, JSONObject finishParam) throws Exception {
        SortedMap<String, String> paramMap = new TreeMap<String, String>();
        paramMap.put("appid", smallWeChatDto.getAppId());
        paramMap.put("mch_id", smallWeChatDto.getMchId());
        paramMap.put("nonce_str", PayUtil.makeUUID(32));
        paramMap.put("transaction_id", storeOrderDto.getPayOrderId());
        //paramMap.put("out_order_no", "FZ" + storeOrderDto.getOrderId());
        paramMap.put("out_order_no", finishParam.getString("out_order_no"));
        paramMap.put("description", "finish");

        String paySwitch = MappingCache.getValue(DOMAIN_WECHAT_PAY, WECHAT_SERVICE_PAY_SWITCH);
        if (WECHAT_SERVICE_PAY_SWITCH_ON.equals(paySwitch)) {
            paramMap.put("appid", MappingCache.getValue(DOMAIN_WECHAT_PAY, WECHAT_SERVICE_APP_ID));  //服务商appid，是服务商注册时公众号的id
            paramMap.put("mch_id", MappingCache.getValue(DOMAIN_WECHAT_PAY, WECHAT_SERVICE_MCH_ID));  //服务商商户号
            //paramMap.put("sub_appid", smallWeChatDto.getAppId());//起调小程序appid
            paramMap.put("sub_mch_id", smallWeChatDto.getMchId());//起调小程序的商户号
        }
        paramMap.put("sign", PayUtil.createSignSha256(paramMap, smallWeChatDto.getPayPassword()));

        //转换为xml
        String xmlData = PayUtil.mapToXml(paramMap);

        JSONObject paramOut = request(FINISH_RECEIVER_URL, xmlData, smallWeChatDto, paramMap.get("mch_id"));
        logger.debug("请求分账 信息{}，返回分账信息{}", xmlData, paramOut.toJSONString());
    }


    private void DeleteReceivers(SmallWeChatDto smallWeChatDto, List<StoreOrderLedgerAcctDto> storeOrderLedgerAcctDtos) throws Exception {

        JSONArray deleteParams = new JSONArray();
        JSONObject param = null;
        Double amount = 0.0;
        for (StoreOrderLedgerAcctDto tmpStoreOrderLedgerAcctDto : storeOrderLedgerAcctDtos) {
            if (StoreOrderLedgerAcctDto.FEE_SPEC_PRODUCT.equals(tmpStoreOrderLedgerAcctDto.getFeeSpecCd())) {
                continue;
            }
            if (StoreOrderLedgerAcctDto.FEE_SPEC_OWNER.equals(tmpStoreOrderLedgerAcctDto.getFeeSpecCd())) {
                continue;
            }
            amount = Double.parseDouble(tmpStoreOrderLedgerAcctDto.getAmount());
            if (amount <= 0) {
                continue;
            }
            JSONObject paramIn = new JSONObject();
            paramIn.put("type", "MERCHANT_ID");
            paramIn.put("account", tmpStoreOrderLedgerAcctDto.getPayAcctId());
            deleteParams.add(paramIn);
        }
        SortedMap<String, String> paramMap = new TreeMap<String, String>();
        paramMap.put("appid", smallWeChatDto.getAppId());
        paramMap.put("mch_id", smallWeChatDto.getMchId());
        paramMap.put("nonce_str", PayUtil.makeUUID(32));

        String paySwitch = MappingCache.getValue(DOMAIN_WECHAT_PAY, WECHAT_SERVICE_PAY_SWITCH);
        if (WECHAT_SERVICE_PAY_SWITCH_ON.equals(paySwitch)) {
            paramMap.put("appid", MappingCache.getValue(DOMAIN_WECHAT_PAY, WECHAT_SERVICE_APP_ID));  //服务商appid，是服务商注册时公众号的id
            paramMap.put("mch_id", MappingCache.getValue(DOMAIN_WECHAT_PAY, WECHAT_SERVICE_MCH_ID));  //服务商商户号
            // paramMap.put("sub_appid", smallWeChatDto.getAppId());//起调小程序appid
            paramMap.put("sub_mch_id", smallWeChatDto.getMchId());//起调小程序的商户号
        }
        paramMap.put("receiver", deleteParams.toJSONString());
        paramMap.put("sign", PayUtil.createSignSha256(paramMap, smallWeChatDto.getPayPassword()));
        //转换为xml
        String xmlData = PayUtil.mapToXml(paramMap);

        logger.debug("调用添加接收方接口" + xmlData);
        ResponseEntity<String> responseEntity = outRestTemplate.postForEntity(
                DELETE_RECEIVER_URL, xmlData, String.class);
        logger.debug("添加接收方接口返回" + responseEntity);

    }

    private JSONArray AddReceivers(SmallWeChatDto smallWeChatDto, List<StoreOrderLedgerAcctDto> storeOrderLedgerAcctDtos) throws Exception {
        JSONArray params = new JSONArray();
        JSONArray addParams = new JSONArray();
        JSONObject param = null;
        Double amount = 0.0;
        for (StoreOrderLedgerAcctDto tmpStoreOrderLedgerAcctDto : storeOrderLedgerAcctDtos) {
            if (StoreOrderLedgerAcctDto.FEE_SPEC_PRODUCT.equals(tmpStoreOrderLedgerAcctDto.getFeeSpecCd())) {
                continue;
            }
            //todo 抵扣金也不分账了
            if (StoreOrderLedgerAcctDto.FEE_SPEC_OWNER.equals(tmpStoreOrderLedgerAcctDto.getFeeSpecCd())) {
                continue;
            }
            amount = Double.parseDouble(tmpStoreOrderLedgerAcctDto.getAmount());
            if (amount <= 0) {
                continue;
            }
            param = new JSONObject();
            param.put("type", "MERCHANT_ID");
            param.put("account", tmpStoreOrderLedgerAcctDto.getPayAcctId());
            param.put("amount", PayUtil.moneyToInteger(amount));
            param.put("description", "商品服务费");

            params.add(param);

            JSONObject paramIn = new JSONObject();
            paramIn.put("type", "MERCHANT_ID");
            paramIn.put("account", tmpStoreOrderLedgerAcctDto.getPayAcctId());
            paramIn.put("name", tmpStoreOrderLedgerAcctDto.getPayAcctName());
            paramIn.put("relation_type", "PARTNER");

            SortedMap<String, String> paramMap = new TreeMap<String, String>();
            paramMap.put("appid", smallWeChatDto.getAppId());
            paramMap.put("mch_id", smallWeChatDto.getMchId());
            paramMap.put("nonce_str", PayUtil.makeUUID(32));

            String paySwitch = MappingCache.getValue(DOMAIN_WECHAT_PAY, WECHAT_SERVICE_PAY_SWITCH);
            if (WECHAT_SERVICE_PAY_SWITCH_ON.equals(paySwitch)) {
                paramMap.put("appid", MappingCache.getValue(DOMAIN_WECHAT_PAY, WECHAT_SERVICE_APP_ID));  //服务商appid，是服务商注册时公众号的id
                paramMap.put("mch_id", MappingCache.getValue(DOMAIN_WECHAT_PAY, WECHAT_SERVICE_MCH_ID));  //服务商商户号
                //paramMap.put("sub_appid", smallWeChatDto.getAppId());//起调小程序appid
                paramMap.put("sub_mch_id", smallWeChatDto.getMchId());//起调小程序的商户号
            }
            paramMap.put("receiver", paramIn.toJSONString());
            paramMap.put("sign", PayUtil.createSignSha256(paramMap, smallWeChatDto.getPayPassword()));
            //转换为xml
            String xmlData = PayUtil.mapToXml(paramMap);

            logger.debug("调用添加接收方接口" + xmlData);
            ResponseEntity<String> responseEntity = outRestTemplate.postForEntity(
                    ADD_RECEIVER_URL, xmlData, String.class);
            logger.debug("添加接收方接口返回" + responseEntity);

            System.out.println("调用添加接收方接口" + xmlData + ";添加接收方接口返回" + responseEntity);
        }
        return params;
    }


    private JSONObject request(String url, String reqData, SmallWeChatDto smallWeChatDto, String key) {

        ByteArrayInputStream inputStream = new ByteArrayInputStream(getPkcs12(smallWeChatDto.getCertPath()));
        JSONObject paramOut = null;
        CloseableHttpClient httpclient = null;
        CloseableHttpResponse response = null;
        try {
            KeyStore keyStore = KeyStore.getInstance("PKCS12");
            //这里写密码..默认是你的MCHID
            keyStore.load(inputStream, key.toCharArray());

            SSLContext sslcontext = SSLContexts.custom()
                    //这里也是写密码的
                    .loadKeyMaterial(keyStore, key.toCharArray())
                    .build();
            SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(
                    sslcontext,
                    SSLConnectionSocketFactory.getDefaultHostnameVerifier());
            httpclient = HttpClients.custom()
                    .setSSLSocketFactory(sslsf)
                    .build();
            String jsonStr = "";

            HttpPost httpost = new HttpPost(url);
            httpost.setEntity(new StringEntity(reqData, "UTF-8"));
            httpost.setHeader("Accept", "text/xml, text/plain");
            httpost.setHeader("Content-Type", "text/xml; charset=utf-8");
            httpost.setHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36");
            response = httpclient.execute(httpost);

            HttpEntity entity = response.getEntity();
            //接受到返回信息
            jsonStr = EntityUtils.toString(response.getEntity(), "UTF-8");
            EntityUtils.consume(entity);
            System.out.println("调用添加接收方接口" + reqData + ";添加接收方接口返回" + jsonStr);
            paramOut = JSONObject.parseObject(JSONObject.toJSONString(PayUtil.xmlStrToMap(jsonStr)));
        } catch (Exception e) {
            logger.error("请求微信异常", e);
            throw new IllegalArgumentException(e.getMessage());
        } finally {
            if (httpclient != null) {
                try {
                    httpclient.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (response != null) {
                try {
                    response.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

        return paramOut;

    }

    private byte[] getPkcs12(String fileName) {
        List<FileDto> fileDtos = new ArrayList<>();

        byte[] context = null;
        String ftpPath = MappingCache.getValue(FtpUploadTemplate.FTP_DOMAIN, FtpUploadTemplate.FTP_PATH);

        String ossSwitch = MappingCache.getValue(MappingConstant.FILE_DOMAIN, OSSUtil.OSS_SWITCH);
        if (StringUtil.isEmpty(ossSwitch) || !OSSUtil.OSS_SWITCH_OSS.equals(ossSwitch)) {
            String ftpServer = MappingCache.getValue(FtpUploadTemplate.FTP_DOMAIN, FtpUploadTemplate.FTP_SERVER);
            int ftpPort = Integer.parseInt(MappingCache.getValue(FtpUploadTemplate.FTP_DOMAIN, FtpUploadTemplate.FTP_PORT));
            String ftpUserName = MappingCache.getValue(FtpUploadTemplate.FTP_DOMAIN, FtpUploadTemplate.FTP_USERNAME);
            String ftpUserPassword = MappingCache.getValue(FtpUploadTemplate.FTP_DOMAIN, FtpUploadTemplate.FTP_USERPASSWORD);
            context = ftpUploadTemplate.downFileByte(ftpPath, fileName, ftpServer,
                    ftpPort, ftpUserName,
                    ftpUserPassword);
        } else {
            context = ossUploadTemplate.downFileByte(ftpPath, fileName, "",
                    0, "",
                    "");
        }

        return context;
    }


    /**
     * 将抵扣金 存到小区账户
     *
     * @param storeOrderLedgerAcctDtos
     */
    private void preStoreCommunityGold(List<StoreOrderLedgerAcctDto> storeOrderLedgerAcctDtos) {
        StoreOrderLedgerAcctDto storeOrderLedgerAcctDto = null;

        for (StoreOrderLedgerAcctDto tmpStoreOrderLedgerAcctDto : storeOrderLedgerAcctDtos) {
            if (!StoreOrderLedgerAcctDto.FEE_SPEC_OWNER.equals(tmpStoreOrderLedgerAcctDto.getFeeSpecCd())) {
                continue;
            }
            storeOrderLedgerAcctDto = tmpStoreOrderLedgerAcctDto;
        }

        if (storeOrderLedgerAcctDto == null) {
            return;
        }

        goldV1InnerServiceSMOImpl.preStoreGold(storeOrderLedgerAcctDto);
    }

    private void prePersonIntegral(List<StoreOrderLedgerAcctDto> storeOrderLedgerAcctDtos) {


        StoreOrderIntegralDto storeOrderIntegralDto = new StoreOrderIntegralDto();
        storeOrderIntegralDto.setOrderId(storeOrderLedgerAcctDtos.get(0).getOrderId());
        storeOrderIntegralDto.setState(StoreOrderIntegralDto.STATE_WAIT);
        List<StoreOrderIntegralDto> storeOrderIntegralDtos = storeOrderIntegralV1InnerServiceSMOImpl.queryStoreOrderIntegrals(storeOrderIntegralDto);
        if (ListUtil.isNull(storeOrderIntegralDtos)) {
            return;
        }


        UserDto userDto = new UserDto();
        userDto.setUserId(storeOrderIntegralDtos.get(0).getPersonId());
        List<UserDto> userDtos = userV1InnerServiceSMOImpl.queryUsers(userDto);
        if (ListUtil.isNull(userDtos)) {
            return;
        }


        IntegralDto integralDto = new IntegralDto();
        integralDto.setLink(userDtos.get(0).getTel());
        integralDto.setObjType(IntegralDto.OBJ_TYPE_PERSON);
        List<IntegralDto> integralDtos = integralV1InnerServiceSMOImpl.queryIntegrals(integralDto);

        if (ListUtil.isNull(integralDtos)) {
            throw new IllegalArgumentException("积分账户不存在");
        }


        PreStoreUserIntegralDto preStoreUserIntegralDto = new PreStoreUserIntegralDto();

        preStoreUserIntegralDto.setIntegralId(integralDtos.get(0).getIntegralId());
        preStoreUserIntegralDto.setAmount(Double.parseDouble(storeOrderIntegralDtos.get(0).getAmount()));
        preStoreUserIntegralDto.setOrderId(storeOrderIntegralDtos.get(0).getOrderId());
        preStoreUserIntegralDto.setRemark(storeOrderIntegralDtos.get(0).getRemark());
        integralV1InnerServiceSMOImpl.preStoreUserIntegral(preStoreUserIntegralDto);

    }


}
